package com.lxb.store.controller;

import com.lxb.store.entity.Goods;
import com.lxb.store.entity.User;
import com.lxb.store.service.GoodsService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class ShopController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/shop")
    public String shopIndex(Model model,
                            @PageableDefault(size = 4,sort = {"recommend"},direction = Sort.Direction.DESC)
                                    Pageable pageable){
        Page<Goods> page = goodsService.findAll(pageable);
        //获取登录用户信息
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        //密码设置为空
        if(loginUser != null){
            loginUser.setPassword(null);
        }
        model.addAttribute("user",loginUser);
        model.addAttribute("page",page);
        return "shop";
    }
}
